import os
import cv2

import RotateImg
import utils
import random
import numpy as np
from tqdm import tqdm

path_in = r'/media/fang/TOSHIBA EXT/Temp/300X300'
path_out = r'/media/fang/TOSHIBA EXT/Temp/600X300/M'
input_img_shape = (300, 300, 3)
out_img_shape = (300, 600, 3)

NameNum = 0
ContainsNum = int(out_img_shape[1] / input_img_shape[1])
names = []
for root, dirs, files in os.walk(path_in):
    for file in files:
        if file.split('.')[-1] == 'jpg':
            names.append(root + '/' + file)

for n in range(2):
    random.shuffle(names)

    if len(names) % ContainsNum:
        img_total_num = int(len(names) / ContainsNum) + 1
    else:
        img_total_num = int(len(names) / ContainsNum)

    for index_pic in tqdm(range(img_total_num - 1)):
        NameNum += 1
        Infos_new = [path_out + '/NT%05d.xml' % NameNum]

        img1 = utils.imread(names[index_pic])
        img2 = utils.imread(names[index_pic + img_total_num])
        Infos1 = utils.ReadXml(names[index_pic][:-3] + 'xml')
        Infos2 = utils.ReadXml(names[index_pic + img_total_num][:-3] + 'xml')



        for info in Infos1:
            Infos_new.append([info[0], info[1], info[2], info[3], info[4]])
        for info in Infos2:
            Infos_new.append([info[0] + 300, info[1], info[2] + 300, info[3], info[4]])

        utils.WriteXml(Infos_new, out_img_shape[1], out_img_shape[0])
        utils.imwrite(Infos_new[0][:-3] + 'jpg', np.hstack((img1, img2)))
        pass
